package com.ujn.zhxy.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ujn.zhxy.pojo.Student;
import com.ujn.zhxy.service.StudentService;
import com.ujn.zhxy.util.MD5;
import com.ujn.zhxy.util.Result;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController //注意 不是@Controller ，异步交互的方法
@RequestMapping("/sms/studentController")
public class StudentController {

    @Autowired
    StudentService studentService;

    @ApiOperation("删除学生信息")
    @DeleteMapping("/DeleteStudentById")
    public Result deleteStudentById(
            @ApiParam("要删除的学生id的json数组") @RequestBody List<Integer> ids
            ){
        studentService.removeByIds(ids);
        return Result.ok();
    }


    @ApiOperation("添加or修改学生信息")
    @PostMapping("/addOrUpdateStudent")
    public Result addOrUpdateStudent(
            @ApiParam("学生信息,json") @RequestBody Student student
    ){
        Integer id = student.getId();
        if (null == id || 0 == id) {
            student.setPassword(MD5.encrypt(student.getPassword()));
        }
        studentService.saveOrUpdate(student);
        return Result.ok();
    }

    @ApiOperation("分页带条件查询学生信息")
    @GetMapping("/getStudentByOpr/{pageNo}/{pageSize}")
    public Result getStudentByOpr(
            @ApiParam("页码数") @PathVariable("pageNo") Integer pageNo,
            @ApiParam("页大小") @PathVariable("pageSize") Integer pageSize,
            @ApiParam("查询的条件") Student student
            ){
        // 分页信息封装page对象
        Page<Student> page = new Page<>(pageNo, pageSize);
        IPage<Student> studentRs = studentService.getStudentByOpr(page,student);
        return Result.ok(studentRs);
    }
}
